
package TimeToWork.Android;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Iterator;

public class DCDay implements Parcelable {

    private MyTime day;

    private ArrayList<WorkTimePeriod> workTimePeriods = new ArrayList<WorkTimePeriod>();

    private ArrayList<Absence> absences = new ArrayList<Absence>();

    private ArrayList<Overtime> overtimes = new ArrayList<Overtime>();

    public DCDay(MyTime day) {
        this.day = day;
    }

    public DCDay(Parcel in) {
        in.readTypedList(this.workTimePeriods, WorkTimePeriod.CREATOR);
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeParcelable(this.day, flags);
        // TODO: Funkar detta!?
        out.writeTypedList(this.workTimePeriods);
        // out.writeTypedList(this.absences);
        // out.writeTypedList(this.overtimes);
    }

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<DCDay> CREATOR = new Parcelable.Creator<DCDay>() {
        public DCDay createFromParcel(Parcel in) {
            return new DCDay(in);
        }

        public DCDay[] newArray(int size) {
            return new DCDay[size];
        }
    };

    public MyTime getDateDay() {
        return this.day;
    }

    public void addWorkTimePeriod(WorkTimePeriod workTimePeriod) {
        this.workTimePeriods.add(workTimePeriod);
    }

    public void addEvent(Event event) {
        if (event instanceof Absence) {
            this.absences.add((Absence)event);
        } else if (event instanceof Overtime) {
            this.overtimes.add((Overtime)event);
        }
    }

    public TimePeriodLength getTotWorkTime() {
        TimePeriodLength totWorkTime = new TimePeriodLength();

        for (Iterator<WorkTimePeriod> i = this.workTimePeriods.iterator(); i.hasNext();) {
            totWorkTime.addTime(i.next().getWorkTime());
        }
        return totWorkTime;

    }

    public String getStrDate(MyTime.TimeFormat format) {
        return this.day.toString(format);
    }

    public String getStrDayTotWorkTime(TimePeriodLength.TimeFormat format) {
        return this.getTotWorkTime().toString(format);
    }

    public TimePeriodLength getTotAbsence() {

        TimePeriodLength totAbsence = new TimePeriodLength();

        for (Iterator<Absence> i = this.absences.iterator(); i.hasNext();) {
            totAbsence.addTime(i.next().getLength());
        }
        return new TimePeriodLength();
    }

    public int getNofWorkTimePeriods() {
        return this.workTimePeriods.size();
    }
}
